package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.AdminMessagesDao;
import ex.com.jm.food.entity.Messages;

/**
 * 
 * @author zhaojw
 *
 */
public class AdminMessagesDaoImpl extends HibernateDaoSupport implements
		AdminMessagesDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Messages> getMessages(final int initPage,final int pageRow,final String value,
			final String s_date,final String e_date) {
		// TODO Auto-generated method stub
		Object mess=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql=" from Messages mess where 1=1 ";
				if(value!=null&&!value.equals("")){
					hql+=" and mess.messValue like '%"+value+"%'";
				}
				if(s_date!=null&&!s_date.equals("")){
					hql+=" and mess.startDate>="+s_date;
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and mess.startDate<="+e_date;
				}
				hql+=" order by mess.startDate desc";
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<Messages>) mess;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int messCount(final String value,final String s_date,final String e_date) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Messages mess where 1=1 ";
				if(value!=null&&!value.equals("")){
					hql+=" and mess.messValue like '%"+value+"%'";
				}
				if(s_date!=null&&!s_date.equals("")){
					hql+=" and mess.startDate>="+s_date;
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and mess.startDate<="+e_date;
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@Override
	public Object saveMess(Messages messages) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(messages);
	}

	@Override
	public void updateMess(Messages messages) {
		// TODO Auto-generated method stub
         this.getHibernateTemplate().update(messages);
	}

	@Override
	public Messages editMess(Integer id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Messages.class, id);
	}

	@Override
	public void delMess(Integer id) {
		// TODO Auto-generated method stub
		 this.getHibernateTemplate().delete(editMess(id));
	}

}
